package com.mlj.controller;

import com.mlj.pojo.Bed;
import com.mlj.pojo.Dormitory;
import com.mlj.service.BedService;
import com.mlj.service.DormitoryService;
import com.mlj.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    DormitoryService dormitoryService;
    @Autowired
    BedService bedService;
    /**
     * 宿舍列表页面
     */
    @RequestMapping("/dormitorylist_page")
    public String dormitoryListPage(){
        return "dormitorylist";
    }
    /**
     * 宿舍信息添加页面
     */
    @RequestMapping("/adddormitory_page")
    public String addDormitoryPage(){
        return "adddormitory";
    }
    /**
     * 获取公寓名称
     */
    @RequestMapping("/dormitoryName")
    @ResponseBody// json格式数据
    public List<Dormitory> getDormitoryName(){
        return dormitoryService.getDormitoryName();
    }
    /**
     *
     * 分页查询列表
     */
    @RequestMapping("/dormitorylist")
    @ResponseBody// json格式数据
    public Result dormitoryList(Integer page, Integer limit){
        return dormitoryService.getDormitoryList(page,limit);
    }
    /**
     * 根据id删除
     */
    @RequestMapping("/delDormitoryById")
    @ResponseBody
    public Result delDormitoryById(Integer id){
        return dormitoryService.delDormitoryById(id);
    }
    /**
     * 批量删除
     */
    @RequestMapping("/deleteBatch")
    @ResponseBody
    public Result deleteBatch(Integer [] ids){
        return dormitoryService.deleteBatch(ids);
    }
    /**
     * 添加公寓信息
     */
    @RequestMapping("/adddormitory")
    @ResponseBody
    public Result addDormitory(Dormitory dormitory){
        return  dormitoryService.addDormitory(dormitory);
    }
    /**
     * 添加寝室长信息
     */
    @RequestMapping("/addHeader")
    @ResponseBody
    public Result addHeader(Integer dormitoryId ,String dormitoryHeader){
        return  dormitoryService.addHeader(dormitoryId,dormitoryHeader);
    }
    /**
     * 修改公寓信息页面
     */
    @RequestMapping("/updateDormitoryById")
    public String updateDormitory(Integer dormitoryId,Integer dormitoryCode,String dormitoryApartment, Model model){
        //System.out.println(dormitoryId);
        //System.out.println(dormitoryCode);
        //System.out.println(dormitoryApartment);
        List<Bed> bedList= bedService.getHeader(dormitoryCode,dormitoryApartment);
        Dormitory dormitory = dormitoryService.selectDormitoryById(dormitoryId);
        model.addAttribute("bedList",bedList);
        model.addAttribute("dormitory",dormitory);
        return "updatedormitory";
    }
    /**
     * 修改公寓信息
     */
    @RequestMapping("/updatedormitory")
    @ResponseBody
    public Result updateDormitory(Dormitory dormitory){
        return  dormitoryService.updateDormitory(dormitory);
    }
    /**
     *条件模糊查询
     */
    @RequestMapping("/search")
    @ResponseBody
    public Result search(String name,Integer page,Integer limit){
        return dormitoryService.search(name,page,limit);
    }
}
